package com.zyd.recyclerviewdemo;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.zyd.recyclerviewdemo.adapter.MyListViewAdapter;
import com.zyd.recyclerviewdemo.view.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

public class ListViewRefreshActivity extends AppCompatActivity implements PullToRefreshView.OnFooterRefreshListener,PullToRefreshView.OnHeaderRefreshListener {
    PullToRefreshView pull_to_refresh;
    ListView listView;
    MyListViewAdapter myListViewAdapter;

    Handler handler=new Handler();
    private List<String> dataList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview_refresh);
        pull_to_refresh=  findViewById(R.id.pull_to_refresh);
        getData();
        /*初始化listView*/
        listView = findViewById(R.id.list1);
        myListViewAdapter = new MyListViewAdapter(dataList,this);

        /*为ListView设置Adapter*/
        listView.setAdapter(myListViewAdapter);

        pull_to_refresh.setOnFooterRefreshListener(this);
        pull_to_refresh.setOnHeaderRefreshListener(this);
    }


    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        Log.w("加载完成","加载完成");

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
                myListViewAdapter.notifyDataSetChanged();
                pull_to_refresh.onFooterRefreshComplete();
            }
        },2000);

    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        Log.w("刷新完成","刷新完成");


        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (dataList.size()>0){
                    dataList.clear();
                    getData();
                }
                pull_to_refresh.onHeaderRefreshComplete();
            }
        }, 2000);

    }

    private void getData() {
        String letter = "Item";
        for (int i = 0; i < 30; i++) {
            dataList.add(letter + i);
        }
    }
}
